package top.chmin.project.cloud.common.entity;

import cn.hutool.core.collection.CollectionUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import top.chmin.project.cloud.builder.*;
import top.chmin.project.cloud.common.column.code.auto.ColumnCode;
import top.chmin.project.cloud.common.column.code.auto.ColumnCodeDao;
import top.chmin.project.cloud.common.column.code.auto.ColumnCodeFPC;
import top.chmin.project.cloud.common.config.auto.Config;
import top.chmin.project.cloud.common.config.auto.ConfigDao;
import top.chmin.project.cloud.common.config.auto.ConfigFPC;
import top.chmin.project.cloud.common.entity.api.BuilderCode;
import top.chmin.project.cloud.common.entity.auto.Entity;
import top.chmin.project.cloud.common.entity.auto.EntityDao;
import top.chmin.project.cloud.common.entity.auto.EntityFPC;
import top.chmin.project.cloud.common.entity.column.auto.EntityColumn;
import top.chmin.project.cloud.common.entity.column.auto.EntityColumnDao;
import top.chmin.project.cloud.common.entity.column.auto.EntityColumnFPC;
import top.chmin.project.cloud.common.entity.column.relation.auto.EntityColumnRelation;
import top.chmin.project.cloud.common.entity.column.relation.auto.EntityColumnRelationDao;
import top.chmin.project.cloud.common.entity.column.relation.auto.EntityColumnRelationFPC;
import top.chmin.project.cloud.common.entity.label.value.auto.EntityLabelValue;
import top.chmin.project.cloud.common.entity.label.value.auto.EntityLabelValueDao;
import top.chmin.project.cloud.common.entity.label.value.auto.EntityLabelValueFPC;

import java.util.List;

/**
 * chmin
 * 2023/12/18 16:48
 * cloud-builder
 */
@Slf4j
@Service
public class ExtEntityService {
    @Resource
    ConfigDao configDao;
    @Resource
    EntityDao entityDao;
    @Resource
    EntityColumnDao entityColumnDao;
    @Resource
    EntityLabelValueDao entityLabelValueDao;
    @Resource
    EntityColumnRelationDao entityColumnRelationDao;
    @Resource
    ColumnCodeDao columnCodeDao;
    @Resource
    ControllerBuilder controllerBuilder;
    @Resource
    DeleteByIdBuilder deleteByIdBuilder;
    @Resource
    ModelBuilder modelBuilder;
    @Resource
    ModelDaoBuilder modelDaoBuilder;
    @Resource
    ModelFPCBuilder modelFPCBuilder;
    @Resource
    PluginBuilder pluginBuilder;
    @Resource
    SaveBuilder saveBuilder;
    @Resource
    SearchBuilder searchBuilder;
    @Resource
    SelectBuilder selectBuilder;
    @Resource
    SelectByIdBuilder selectByIdBuilder;
    @Resource
    SelectPageBuilder selectPageBuilder;
    @Resource
    ServiceBuilder serviceBuilder;
    @Resource
    UpdateByIdBuilder updateByIdBuilder;
    @Resource
    PageUiBuilder pageUiBuilder;
    @Resource
    PageConfigBuilder pageConfigBuilder;
    @Resource
    PageColumnInitBuilder pageColumnInitBuilder;
    @Resource
    PageDetailInitBuilder pageDetailInitBuilder;
    @Resource
    PageSaveInitBuilder pageSaveInitBuilder;
    @Resource
    PageSearchInitBuilder pageSearchInitBuilder;
    @Resource
    PageUpdateInitBuilder pageUpdateInitBuilder;

    public void build(BuilderCode.Param param) {
        if (StringUtils.isEmpty(param.getTableCode())) {
            return;
        }
        if (StringUtils.isEmpty(param.getConfigCode())) {
            return;
        }
        Config config = configDao.getFirst(
                new ConfigFPC()
                        .configCode().eq(param.getConfigCode())
                        .toCondition()
        );
        if (config == null) {
            return;
        }
        Entity entity = entityDao.getFirst(
                new EntityFPC()
                        .tableCode().eq(param.getTableCode())
                        .toCondition());
        if (entity == null) {
            return;
        }
        List<EntityColumn> entityColumnList = entityColumnDao.getStreamByCondition(
                new EntityColumnFPC()
                        .tableCode().eq(param.getTableCode())
                        .toCondition()
        ).toList();
        if (CollectionUtil.isEmpty(entityColumnList)) {
            return;
        }

        List<EntityLabelValue> entityLabelValueList = entityLabelValueDao.getStreamByCondition(
                new EntityLabelValueFPC()
                        .tableCode().eq(param.getTableCode())
                        .toCondition()
        ).toList();

        List<EntityColumnRelation> entityColumnRelationList = entityColumnRelationDao.getStreamByCondition(
                new EntityColumnRelationFPC()
                        .sourceTableCode().eq(param.getTableCode())
                        .toCondition()
        ).toList();

        List<ColumnCode> columnCodeList = columnCodeDao.getStreamByCondition(
                new ColumnCodeFPC()
                        .fieldCode().in(entityColumnList.stream().map(EntityColumn::getFieldCode).toList())
                        .toCondition()
        ).toList();

        BuilderConfig builderConfig = new BuilderConfig()
                .setConfig(config)
                .setEntity(entity)
                .setEntityColumnList(entityColumnList)
                .setEntityLabelValueList(entityLabelValueList)
                .setEntityColumnRelationList(entityColumnRelationList)
                .setColumnCodeList(columnCodeList);

        controllerBuilder.build(builderConfig);
        deleteByIdBuilder.build(builderConfig);
        modelBuilder.build(builderConfig);
        modelDaoBuilder.build(builderConfig);
        modelFPCBuilder.build(builderConfig);
        pluginBuilder.build(builderConfig);
        saveBuilder.build(builderConfig);
        searchBuilder.build(builderConfig);
        selectBuilder.build(builderConfig);
        selectByIdBuilder.build(builderConfig);
        selectPageBuilder.build(builderConfig);
        serviceBuilder.build(builderConfig);
        updateByIdBuilder.build(builderConfig);
        pageUiBuilder.build(builderConfig);
        pageConfigBuilder.build(builderConfig);
        pageSearchInitBuilder.build(builderConfig);
        pageSaveInitBuilder.build(builderConfig);
        pageUpdateInitBuilder.build(builderConfig);
        pageDetailInitBuilder.build(builderConfig);
        pageColumnInitBuilder.build(builderConfig);

    }
}
